/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.netbeans.modules.web.stripes.wizards;

import javax.swing.JPanel;
import org.netbeans.modules.web.stripes.util.StringUtils;
import org.netbeans.modules.web.stripes.util.browse.BrowseDialogs;
import org.netbeans.spi.project.ui.templates.support.Templates;
import org.openide.WizardDescriptor;

public final class LayoutDefinitionVisualPanel1 extends JPanel {

    private LayoutDefinitionWizardPanel wizardPanel;
    private WizardDescriptor wizard;
    
    /** Creates new form LayoutDefinitionVisualPanel1 */
    public LayoutDefinitionVisualPanel1(
            LayoutDefinitionWizardPanel wizardPanel,
            WizardDescriptor wizard) {
        
        this.wizardPanel = wizardPanel;
        this.wizard = wizard;
        
        initComponents();
    }

    @Override
    public String getName() {
        return "Layout Definition";
    }

    public String getDefinitionName(){
        return definitionName.getText();
    }
    
    public boolean isInputValid(){
        String layoutDefinitionName = getDefinitionName();
        
        // TODO validate the syntax "/aaaa/bbb/something.jsp"
        
        return !StringUtils.nullOrEmpty(layoutDefinitionName) &&
                layoutDefinitionName.startsWith("/") &&
                layoutDefinitionName.endsWith(".jsp");
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        definitionName = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(LayoutDefinitionVisualPanel1.class, "LayoutDefinitionVisualPanel1.jLabel1.text")); // NOI18N

        definitionName.setText(org.openide.util.NbBundle.getMessage(LayoutDefinitionVisualPanel1.class, "LayoutDefinitionVisualPanel1.definitionName.text")); // NOI18N
        definitionName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                definitionNameKeyReleased(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(LayoutDefinitionVisualPanel1.class, "LayoutDefinitionVisualPanel1.jButton1.text")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(definitionName, javax.swing.GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(definitionName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(246, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void definitionNameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_definitionNameKeyReleased
    wizardPanel.fireChangeEvent();
}//GEN-LAST:event_definitionNameKeyReleased

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    String selectedJspPath = BrowseDialogs.jspSourcesDialog(
            Templates.getProject(wizard), this);

    if(!StringUtils.nullOrEmpty(selectedJspPath)){
        definitionName.setText(selectedJspPath);
    }
}//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField definitionName;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}

